package com.kdtech.analyse.news;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.analyse.tool.ParseLogic;
import com.kdtech.analyse.tool.ParseTool;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;

public class ChinadailyNewsAnalyse implements AnalyseNews {

	//http://www.chinadaily.com.cn/dfpd/2013-05/06/content_16480219.htm
	private static final String[] regex={
		"http://.*.chinadaily.com.cn/.*[0-9]+-[0-9]+/[0-9]+/content_[0-9]*.htm[l]*",
		"http://.*.chinadaily.com.cn/.*[0-9]+-[0-9]+-[0-9]+/content_[0-9]+.htm[l]*",
	};

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
//		NewsMeta news=new ParseNewsUtils().parse(urlMeta);
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		Document doc=Jsoup.parse(htmltxt);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		ParseTool tool=new ParseTool(doc);

		tool.addParseLogic(new ParseLogic("h2#Position"));
		tool.addParseLogic(new ParseLogic("h1.titlebox"));
		tool.addParseLogic(new ParseLogic("h1.titleTxt32"));
		tool.addParseLogic(new ParseLogic("h1.newtitlebox"));
		title=tool.parse();

		content=JSoupUtils.selectContent(url, doc, "div#Content","div#Zoom");

		author =JSoupUtils.matchAuthor(doc, "来源:");

		tool.addParseLogic(new ParseLogic("div.arcfrom"));
		tool.addParseLogic(new ParseLogic("td[width=116]"));
		tool.addParseLogic(new ParseLogic("span.timebox"));
		tool.addParseLogic(new ParseLogic("span.greyTxt6"));
		tool.addParseLogic(new ParseLogic("td[width=116]"));
		date=tool.parseDate();
		if (date==null){
			date =DateUtils.matchDate(url);
		}

		news.setUrl(url);
		news.setTitle(title);
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);
		return news;
	}

	public static void main(String[] args) {
		String url="http://house.chinadaily.com.cn/2016-08/23/content_26573137.htm";
		ChinadailyNewsAnalyse test=new ChinadailyNewsAnalyse();
		System.out.println(test.isDetailPage(url));
		url=UrlArgumentTop.FromatUrl(url);
		System.out.println(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=test.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}

	public static HashMap<String, String> getLinks(HashMap<String, String> a_list) {
		HashMap<String,String> tmpList =new  HashMap<String,String>();
		Iterator<Entry<String, String>> iterator = a_list.entrySet().iterator();
		while(iterator.hasNext()) {
			Entry<String, String> next = iterator.next();
			String url = next.getKey();
			String value = next.getValue();
			if(StringUtils.isNotBlank(url)){
				String highlight = StringUtils.substringBetween(url, "forum.php?", "&mod=");
				if(StringUtils.isNotBlank(highlight)){
					url = url.replace(highlight+"&", "");
				}
				tmpList.put(url, value);
			}
		}
		return tmpList;
	}
}
